# coding=utf-8
# author=haishan09@gmail.com
from chart.adapters.base import BaseAdapter
from common.provider import provider_register


@provider_register('chartadapter', 'form')
class DataTablesAdapter(BaseAdapter):

    def transform(self, config, source):
        retdict = {
            'sEcho': 0,
            'aaData': [],
            'iTotalRecords': 0,
            'iTotalDisplayRecords': 0,
        }
        dimensions = map(lambda d: d['drilldown'], config['drilldowns'])
        aggregates = map(lambda d: d['aggregate'], config['aggregates'])
        for cell in source['cells']:
            data = []
            for dim_name in dimensions:
                data.append(cell.get(dim_name, 0))
            for agg_name in aggregates:
                data.append(cell.get(agg_name, 0))
            retdict['aaData'].append(data)
        retdict['iTotalRecords'] = source['total_records']
        retdict['iTotalDisplayRecords'] = source['total_records']
        return retdict

